package com.example.demo.date;

import cn.hutool.core.date.DateTime;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * jdk8的日期类型
 * Instant
 * LocalDate
 * LocalTime
 * LocalDateTime
 * DateTimeFormatter
 *
 * @author pengyifeng
 * @version 1.0.0
 * @ClassName DateDemo.java
 * @Description TODO
 * @createTime 2020年11月19日 16:57:00
 */
public class DateDemo {
    public static void main(String[] args) {
        //Instant 代替Date
        //获取当前时间
        Instant now = Instant.now();
        System.out.println(now);
        //2020-11-19T09:00:17.660Z 输出的时间这个时间比北京时间少了8个小时，如果要输出北京时间，可以加上默认时区：
        System.out.println(now.atZone(ZoneId.systemDefault()));
        //获取时间戳 秒
        System.out.println(now.getEpochSecond());
        //获取时间戳 毫秒
        System.out.println(now.toEpochMilli());
        // 将long转换为Instant
        //根据秒数时间戳转换：
        Instant now1 = Instant.now();
        System.out.println(now1);
        long nowEpochSecond = now1.getEpochSecond();
        System.out.println(Instant.ofEpochSecond(nowEpochSecond));
        System.out.println(Instant.ofEpochSecond(nowEpochSecond, now.getNano()));
        //根据毫秒数时间戳转换：
        Instant now2 = Instant.now();
        System.out.println(now2);
        long l = now2.toEpochMilli();
        System.out.println(Instant.ofEpochMilli(l));


        //LocalDate
        //获取当前日期 2020-11-19
        System.out.println(LocalDate.now());

        //获取年月日
        LocalDate localDate = LocalDate.now();
        int year = localDate.getYear();
        int monthValue = localDate.getMonthValue();
        int dayOfMonth = localDate.getDayOfMonth();
        System.out.println("year:" + year);
        System.out.println("monthValue:" + monthValue);
        System.out.println("dayOfMonth:" + dayOfMonth);

        //指定日期
        LocalDate date = LocalDate.of(2020, 01, 01);
        System.out.println("date:" + date);

        //比较日期是否相等
        LocalDate now3 = LocalDate.now();
        LocalDate localDate1 = LocalDate.of(2020, 11, 19);
        System.out.println("比较日期是否相等:" + now3.equals(localDate1));

        //获取日期是本周/本月/本年的第几天
        LocalDate now4 = LocalDate.now();
        System.out.println(now4);
        System.out.println("now4.getDayOfWeek():" + now4.getDayOfWeek());
        System.out.println("今天是本周的第几天" + now4.getDayOfWeek().getValue());
        System.out.println("这是本月的第几天" + now4.getDayOfMonth());
        System.out.println("这是本年的第几天" + now4.getDayOfYear());
        //判断是否为闰年
        LocalDate now5 = LocalDate.now();
        System.out.println(now5.getYear() + "===================" + now5.isLeapYear());

        //LocalTime
        Date date1 = new Date();

        int hour = date1.getHours();
        int minute = date1.getMinutes();
        int second = date1.getSeconds();

        System.out.println("hour: " + hour);
        System.out.println("minute: " + minute);
        System.out.println("second: " + second);

        System.out.println("======================我是分割线==========================");
        Calendar calendar = Calendar.getInstance();

// 12小时制
        int hourOf12 = calendar.get(Calendar.HOUR);
// 24小时制
        int hourOf24 = calendar.get(Calendar.HOUR_OF_DAY);
        int minute1 = calendar.get(Calendar.MINUTE);
        int second1 = calendar.get(Calendar.SECOND);
        int milliSecond = calendar.get(Calendar.MILLISECOND);

        System.out.println("hourOf12: " + hourOf12);
        System.out.println("hourOf24: " + hourOf24);
        System.out.println("minute: " + minute1);
        System.out.println("second: " + second1);
        System.out.println("milliSecond: " + milliSecond);


        System.out.println("======================我是分割线==========================");
        LocalTime localTime = LocalTime.now();
        System.out.println("localTime:" + localTime);

        int hour2 = localTime.getHour();
        int minute2 = localTime.getMinute();
        int second2 = localTime.getSecond();

        System.out.println("hour: " + hour2);
        System.out.println("minute: " + minute2);
        System.out.println("second: " + second2);
        System.out.println("======================我是分割线==========================");
        //LocalDateTime
        //获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localDateTime:" + localDateTime);

        LocalDateTime localDateTime1 = LocalDateTime.now();
        System.out.println("localDateTime: " + localDateTime1);

        System.out.println("year: " + localDateTime1.getYear());
        System.out.println("month: " + localDateTime1.getMonthValue());
        System.out.println("day: " + localDateTime1.getDayOfMonth());
        System.out.println("hour: " + localDateTime1.getHour());
        System.out.println("minute: " + localDateTime1.getMinute());
        System.out.println("second: " + localDateTime1.getSecond());

        //增加天数/小时
        LocalDateTime localDateTime2 = LocalDateTime.now();
        System.out.println("localDateTime: " + localDateTime2);

        LocalDateTime tomorrow = localDateTime2.plusDays(1);
        System.out.println("tomorrow: " + tomorrow);

        LocalDateTime nextHour = localDateTime2.plusHours(1);
        System.out.println("nextHour: " + nextHour);

        //获取时间是本周/本年的第几天
        LocalDateTime localDateTime3 = LocalDateTime.now();
        System.out.println("localDateTime: " + localDateTime3);

        System.out.println("DayOfWeek: " + localDateTime3.getDayOfWeek().getValue());
        System.out.println("DayOfYear: " + localDateTime3.getDayOfYear());

        //DateTimeFormatter 格式化LocalDate
        LocalDate localDate2 = LocalDate.now();

        System.out.println("ISO_DATE: " + localDate2.format(DateTimeFormatter.ISO_DATE));
        System.out.println("BASIC_ISO_DATE: " + localDate2.format(DateTimeFormatter.BASIC_ISO_DATE));
        System.out.println("ISO_WEEK_DATE: " + localDate2.format(DateTimeFormatter.ISO_WEEK_DATE));
        System.out.println("ISO_ORDINAL_DATE: " + localDate2.format(DateTimeFormatter.ISO_ORDINAL_DATE));

        LocalDate localDate3 = LocalDate.now();

        System.out.println("yyyy/MM/dd: " + localDate3.format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));

        //格式化LocalTime
        LocalTime localTime4 = LocalTime.now();
        System.out.println(localTime4);
        System.out.println("ISO_TIME: " + localTime4.format(DateTimeFormatter.ISO_TIME));
        System.out.println("HH:mm:ss: " + localTime4.format(DateTimeFormatter.ofPattern("HH:mm:ss")));

        LocalDateTime localDateTime11 = LocalDateTime.now();
        System.out.println(localDateTime11);
        System.out.println("ISO_DATE_TIME: " + localDateTime11.format(DateTimeFormatter.ISO_DATE_TIME));
        System.out.println("ISO_DATE: " + localDateTime11.format(DateTimeFormatter.ISO_DATE));

//        7. 总结
//        JDK8推出了全新的日期时间类，如Instant、LocaleDate、LocalTime、LocalDateTime、DateTimeFormatter，设计比之前更合理，也是线程安全的。
//
//《阿里巴巴Java开发规范》中也推荐使用Instant代替Date，LocalDateTime 代替 Calendar，DateTimeFormatter 代替 SimpleDateFormat。
//
//        因此，如果条件允许，建议在项目中使用，没有使用的，可以考虑升级下。

        //“Instant“是即时时刻，是代表时间轴中某一特定的时刻点。一般适合替换传统的“java.util.Date"和“java.sql.Timestamp"，用来表示时间戳记。
        //
        //“LocalDateTime“ 表示本地日期和时间，因为不具备时区和UTC偏移的概念，所以无法代表一个特定的时刻，通常被视为年月日和小时分钟秒。一般可以描述像生日的日期，以及在墙上时钟上看到的本地时间。


    }
}
